package cm.example.shoppingplatformspringboot.utils;

import org.apache.tomcat.websocket.AuthenticationException;
import org.jetbrains.annotations.NotNull;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.Map;

@RestControllerAdvice
public class GlobalExceptionHandler {

    // 处理所有未捕获异常
    @ExceptionHandler(Exception.class)
    public ResponseEntity<Map<String, Object>> handleAllExceptions(Exception ex) {
        // 生产环境应记录日志
        return ResponseUtil.generateResponse("error", "系统繁忙，请稍后再试", null);
    }

    // 处理特定业务异常
    @ExceptionHandler(AuthenticationException.class)
    public ResponseEntity<Map<String, Object>> handleAuthException(@NotNull AuthenticationException ex) {
        return ResponseUtil.generateResponse("error", ex.getMessage(), null);
    }
}